<template>
	<view>
		<view class="user_top">
			<view class="left">
				<u--image :src="avatar" width="140rpx" height="140rpx" shape="circle" mode="widthFix"></u--image>
				<view class="text" @click="showForm">{{name || '点击登录'}}</view>
			</view>
			<u-icon :name="iconName" color="#fff" @click="onClickIcon"></u-icon>
		</view>

		<u-popup :show="formVisible" mode="center" :customStyle="{padding:'30rpx',width:'90%',boxSizing:'border-box'}"
			@close="closeForm">
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1" labelWidth="150rpx">
				<u-form-item label="用户名" prop="userInfo.name" borderBottom>
					<!-- 不支持v-model.trim -->
					<u--input v-model="model1.userInfo.name" placeholder="请填写用户名" border="none"></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="userInfo.password" borderBottom>
					<u--input v-model="model1.userInfo.password" placeholder="请填写密码" border="none">
					</u--input>
				</u-form-item>
			</u--form>
			<u-button :customStyle="{marginTop:'30rpx'}" type="primary" text="确定" @click="onSubmit"></u-button>
		</u-popup>

		<u-modal :show="modalVisible" title="提示" content='确认退出登录吗' showCancelButton closeOnClickOverlay
			@confirm="onConfirm" @cancel="onClose" @close="onClose"></u-modal>
	</view>
</template>

<script>
	import {
		apiAuthLoginByWeb
	} from '../../request/api.js'
	import request from '../../request/request.js'
	const defaultAvatar =
		"";

	export default {
		data() {
			return {
				name: '',
				avatar: defaultAvatar,
				iconName: 'arrow-right',
				formVisible: false,
				model1: {
					userInfo: {
						name: '',
						password: '',
					},
				},

				rules: {
					'userInfo.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'userInfo.password': [{
							type: 'string',
							required: true,
							message: '请填写密码',
							trigger: ['blur', 'change']
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return value.length >= 6 && value.length <= 10
							},
							message: '密码应该在6到10位之间',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						},
					]
				},
				modalVisible: false
			};
		},
		onLoad() {
			// 判断缓存里面有没有token和userInfo
			const token = uni.getStorageSync('token')
			const userInfo = uni.getStorageSync('userInfo')
			if (token && userInfo) {
				const {
					username,
					avatar
				} = userInfo

				this.name = username
				this.avatar = avatar
				this.iconName = 'close'
			}
		},
		methods: {
			showForm() {
				const token = uni.getStorageSync('token')
				if (!token) {
					this.formVisible = true;
				}
			},
			// 点击右侧图标
			onClickIcon() {
				const token = uni.getStorageSync('token')
				if (!token) {
					this.formVisible = true;
				} else {
					this.modalVisible = true
				}
			},
			closeForm() {
				this.formVisible = false;
			},
			onSubmit() {
				// 通过调用dom的validate方法返回了一个promise,因此可以写.then .catch
				this.$refs.form1.validate().then(res => {
					// console.log(uni.$u)
					// uni.$u.toast('校验通过')
					// 再去提交表单
					request({
						url: apiAuthLoginByWeb,
						method: 'POST',
						data: {
							username: this.model1.userInfo.name.trim(),
							pwd: this.model1.userInfo.password.trim(),
						},
						successCb: res => {
							console.log(res)
							const {
								token,
								userInfo
							} = res
							uni.setStorageSync('token', token)
							uni.setStorageSync('userInfo', userInfo)
							const {
								username,
								avatar
							} = userInfo
							// 头像和用户名和icon
							this.name = username
							this.avatar = avatar
							this.iconName = 'close'
							// 清空表单
							this.$refs.form1.resetFields()
							this.formVisible = false
						}
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			onClose() {
				this.modalVisible = false
			},
			onConfirm() {
				uni.clearStorageSync() // 清空缓存
				// 头像用户名图标恢复默认值
				this.avatar = defaultAvatar
				this.name = ''
				this.iconName = 'arrow-right'

				this.onClose()
			}
		},
	};
</script>

<!-- scoped开启样式隔离 -->
<style lang="scss" scoped>
	.user_top {
		background: #333;
		color: #fff;
		padding: 40rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.left {
			display: flex;
			align-items: center;

			.text {
				font-size: 26rpx;
				line-height: 3;
				margin-left: 20rpx;
			}
		}
	}

	/deep/ .u-popup__content {
		width: 90%;
	}

	// 开启了scoped的情况下去更改第三方ui库的样式,需要加上/deep/ 
	/deep/ .u-modal__content__text {
		text-align: center;
	}
</style>
